package learning.data;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import learning.data.document.InstanceDocument;
import learning.util.SparseVector;

public class InstanceDataReader {
	static String file = "O:/unix/projects/pardosa/data10/raphaelh/learning/birth_date";

	public static Dataset<InstanceDocument> read(String file, String charset) throws IOException {
		return read(file, charset, -1);
	}

	public static Dataset<InstanceDocument> read(String file, String charset, int maxSize) throws IOException {
		return read(new InputStreamReader(new FileInputStream(file + ".data"), charset), 
				    new InputStreamReader(new FileInputStream(file + ".labels"), charset), 
				    new InputStreamReader(new FileInputStream(file + ".features"), charset), maxSize);
	}
	
	public static Dataset<InstanceDocument> read(Reader isData, Reader isLabels, Reader isFeatures, int maxSize) throws IOException {
		
		Dataset<InstanceDocument> dataset;
		{
			HashMap<String, Integer> featureIds;
			String[] featureNames;			
			HashMap<String, Integer> labelIds;
			String[] labelNames;
			List<InstanceDocument> docs;
			
			{
				// read labels
				labelIds = new HashMap<String, Integer>();
				BufferedReader br = new BufferedReader(isLabels);
				String line = null;
				while ((line = br.readLine()) != null) {
					labelIds.put(line.substring(line.indexOf("\t") + 1), 
							Integer.parseInt(line.substring(0, line.indexOf("\t"))));				
				}
				labelNames = new String[labelIds.size()];
				for (Map.Entry<String, Integer> e : labelIds.entrySet())
					labelNames[e.getValue()] = e.getKey();
				br.close();
			}
			{
				// read features
				featureIds = new HashMap<String, Integer>();
				BufferedReader br = new BufferedReader(isFeatures);
				String line = null;
				while ((line = br.readLine()) != null) {
					//System.out.println(line);
					featureIds.put(line.substring(line.indexOf("\t") + 1), 
							Integer.parseInt(line.substring(0, line.indexOf("\t"))));
				}
				// TEMPORARY :
				featureNames = null;

				//featureNames = new String[featureIds.size()];
				//for (Map.Entry<String, Integer> e : featureIds.entrySet())
				//	featureNames[e.getValue()] = e.getKey();
				br.close();
			}
			{
				// read dataset
				docs = new ArrayList<InstanceDocument>();
				BufferedReader br = new BufferedReader(isData);
				
				String line = null;
				while ((line = br.readLine()) != null) {
					if (maxSize >= 0 && docs.size() >= maxSize) break;
					
					String[] cols = line.split(" ");
					
					String token = cols[0];
					int label = labelIds.get(cols[1]);
					//int label = labelIds.get(cols[cols.length-1]);
					
					int[] fids = new int[cols.length-2];
					float[] fvals = new float[cols.length-2];
					for (int i=2; i < fids.length; i++) {
						String[] p = cols[i].split(":");
						if (p.length == 0) continue;
						fids[i-2] = Integer.parseInt(p[0]); //featureIds.get(cols[i]);
						fvals[i-2] = Float.parseFloat(p[1]); //1.0f;
					}
					SparseVector features = new SparseVector(fids, fvals, fids.length);

					InstanceDocument doc = new InstanceDocument(token, features, label);
					docs.add(doc);
				}
				br.close();
			}
			dataset = new Dataset<InstanceDocument>(docs.toArray(new InstanceDocument[0]),
					featureNames, featureIds, labelNames, labelIds);
		}
		return dataset;
	}
}
